package org.ucas.faker.web.fetcher.op;

import org.ucas.faker.web.fetcher.DynamicSqlException;
import org.ucas.faker.web.fetcher.meta.ColumnField;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Aggr extends ColumnField  implements Serializable {

  public String op;

  private static final Set<String> accepted = new HashSet<String>(){{
    add("COUNT");
    add("SUM");
    add("MIN");
    add("MAX");
    add("AVG");
  }};

  public Aggr(ColumnField column, String op) {
    if(!accepted.contains(op.toUpperCase())) throw new DynamicSqlException("Unsupported aggr op : " + op);
    this.op = op;
    this.column = column.column;
    this.alias = column.alias;
    this.table = column.table;
  }

  @Override
  public String sql() {
    return op == null? super.sql() : op.toUpperCase() + "(" + super.sql() + ")";
  }
}
